package com.xcy.controller;

import com.xcy.dto.Sys_attachment;
import com.xcy.service.ISys_attachmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys_attachment")
public class Sys_attachmentController {
    @Resource
    private ISys_attachmentService sys_attachmentService;

    @RequestMapping("/sys_attachmentList")
    public String sys_attachmentList(HttpServletRequest request,Model model){
        List<Sys_attachment> sList = sys_attachmentService.getAllSys_attachment();
        model.addAttribute("sList", sList);
        return "sys_attachmentList";
    }

    @RequestMapping("/sys_attachmentListByParams")
    public String sys_attachmentListByParams(@RequestParam(value = "content_type", required = true) String content_type,
                                             @RequestParam(value = "create_by", required = true) int create_by,Model model){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("content_type",content_type);
        map.put("create_by",create_by);
        Sys_attachment sys_attachment= sys_attachmentService.querySys_attachmentByParams(map);
        model.addAttribute("sys_attachment", sys_attachment);
        return "sys_attachmentListByParams";
    }

    @RequestMapping("/sys_attachmentListByParamsUI")
    public String sys_attachmentListByParamsUI(){ return "sys_attachmentListByParamsUI";}

    @RequestMapping("/showSys_attachment")
    public String showSys_attachment(HttpServletRequest request,Model model){
        int id = Integer.parseInt(request.getParameter("id"));
        Sys_attachment sys_attachment = sys_attachmentService.getSys_attachmentById(id);
        model.addAttribute("sys_attachment", sys_attachment);
        return "showSys_attachment";
    }

    @RequestMapping("/addSys_attachmentUI")
    public String addSys_attachmentUI(){
        return "addSys_attachment";
    }

    @RequestMapping("/addSys_attachment")
    public String addSys_attachment(HttpServletRequest request,Model model){
        Sys_attachment sys_attachment = new Sys_attachment();
        sys_attachment.setContent_type(String.valueOf(request.getParameter("content_type")));
        sys_attachment.setCreate_by(Integer.valueOf(request.getParameter("create_by")));
        sys_attachment.setCreate_time(String.valueOf(request.getParameter("create_time")));
        sys_attachment.setEnable(Integer.valueOf(request.getParameter("enable")));
        sys_attachment.setFile_name(String.valueOf(request.getParameter("file_name")));
        sys_attachment.setFile_size(Integer.valueOf(request.getParameter("file_size")));
        sys_attachment.setFile_url(String.valueOf(request.getParameter("file_url")));
        sys_attachment.setDel_flag(Integer.valueOf(request.getParameter("del_flag")));

        sys_attachmentService.addSys_attachment(sys_attachment);
        return "redirect:/sys_attachment/sys_attachmentList";
    }

    @RequestMapping(value = "/deleteSys_attachment", method = RequestMethod.POST)
    public String deleteById(@RequestParam(value = "id", required = true) int id){

        sys_attachmentService.deleteSys_attachmentById(id);

        return "redirect:/sys_attachment/sys_attachmentList";
    }

    @RequestMapping(value = "/updateSys_attachment", method = RequestMethod.POST)
    public String updateSys_attachment(@RequestParam(value = "id", required = true) int id,
                             @RequestParam(value = "content_type", required = true) String content_type,
                             @RequestParam(value = "create_by", required = true) int create_by,
                             @RequestParam(value = "create_time", required = true) String create_time,
                             @RequestParam(value = "enable", required = true) int enable,
                             @RequestParam(value = "file_name", required = true) String file_name,
                             @RequestParam(value = "file_size", required = true) int file_size,
                             @RequestParam(value = "file_url", required = true) String file_url,
                             @RequestParam(value = "del_flag", required = true) int del_flag){
        Sys_attachment sys_attachment = new Sys_attachment();
        sys_attachment.setId(id);
        sys_attachment.setContent_type(content_type);
        sys_attachment.setCreate_by(create_by);
        sys_attachment.setCreate_time(create_time);
        sys_attachment.setEnable(enable);
        sys_attachment.setFile_name(file_name);
        sys_attachment.setFile_size(file_size);
        sys_attachment.setFile_url(file_url);
        sys_attachment.setDel_flag(del_flag);

        sys_attachmentService.updateSys_attachment(sys_attachment);

        return "redirect:/sys_attachment/sys_attachmentList";
    }
}